package com.ecr.hub.database.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

import com.ecr.hub.model.composition.Composition;
import com.ecr.hub.model.wagon.Wagon;

public class MapperComposition {

	protected static ParameterizedRowMapper<Composition> mapperComposition = new ParameterizedRowMapper<Composition>() {

		public Composition mapRow(ResultSet rs, int rowNum) throws SQLException {

			Composition composition = new Composition();
			composition.setId(rs.getInt("id"));
			composition.setDestinationExtId(rs.getString("destination_ext_id"));
			composition.setSillonId(rs.getInt("sillon_id"));
			composition.setComment(rs.getString("comment"));
			composition.setRailNumber(rs.getString("number"));
			composition.setPosRailId(rs.getInt("pos_rail_id"));
			composition.setPosRailSaveId(rs.getInt("pos_rail_save_id"));

			// Autres données pour le fonctionnement du hub (visuel).
			composition.setDestinationLabel(rs.getString("destination_label"));
			composition.setDestinationHexColor(rs
					.getString("destination_hex_color"));
			composition.setDestinationTypeBundleId(rs.getInt("bundle_type_id"));

			// Insertion de l'objet wagon dans la composition.
			Wagon wagon = new Wagon();
			wagon.setId(rs.getInt("wagon_id"));
			wagon.setLading(rs.getString("lading"));
			wagon.setNumLot(rs.getInt("num_lot"));
			// En position réelle, on a juste la position dans la rame.
			wagon.getCurrentPos().setPosNum(rs.getInt("pos_num"));
			// En position demandée on à juste la voie.
			wagon.getNextPos().setPosRailId(rs.getInt("pos_rail_id"));
			wagon.setFromRail(rs.getString("pos_from_rail"));
			wagon.setDestinationId(rs.getInt("destination_id"));
			wagon.setExtId(rs.getString("wagon_ext_id"));
			wagon.setHexColor(rs.getString("destination_hex_color"));
			// Destination finale du wagon.
			wagon.setDestinationLabel(rs.getString("wagon_destination_label"));
			wagon.setWeightLading(rs.getFloat("weight_lading"));
			wagon.setTcmsTrainId(rs.getString("tcms_train_id"));
			wagon.getType().setTotalLength(rs.getFloat("total_length"));
			wagon.getType().setCurbWeight(rs.getFloat("curb_weight"));
			wagon.getType().setLabel(rs.getString("type_label"));
			wagon.setSillonExtId(rs.getString("sillon_ext_id"));
			wagon.setSillonId(rs.getInt("sillon_id"));
			wagon.setBrakeForce(rs.getFloat("brake_force"));
			wagon.setAte(rs.getString("ate"));
			composition.setWagon(wagon);
			return composition;
		}
	};
}